package com.csthink.web.infrastructure.security;

import com.csthink.mysql.entity.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * Login completed, Authentication tool kit.
 * @author <a href="mailto:security.2009@live.cn">Mars</a>
 * @since 2023-09-13
 */
public class AuthenticationInfoUtil {

    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    public static String getAccountId() {
        return getLoginUser().getAccountId();
    }

    public static User getLoginUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User loginUser = new User();
        loginUser.setAccountId("unknown");
        loginUser.setId("unknown");
        if (authentication == null || authentication.getPrincipal() == null) {
            return loginUser;
        }

        if (authentication.getPrincipal() instanceof User) {
            loginUser = ((User) authentication.getPrincipal());
        }

        return loginUser;
    }
}
